package site.wuao.simplerxjava.operator;


import android.util.Log;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * 操作符 - 过滤
 *
 * @author wuao
 * @date 2017/12/1
 * @github {https://github.com/CoderWuao}
 * @blogs {http://wuao.site/}
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @github -
 * @blogs -
 * @note -
 */
public class FilterOperator {
    /** 标记 */
    private static final String TAG = "FilterOperator";

    /**
     * filter - 过滤
     *
     * @note 只有返回值为true的才会发射给观察者
     */
    public void filter() {
        Observable
                .just(1, 2, 3, 4, 5)
                .filter(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        return integer > 3;
                    }
                })
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 4
        // call: 5
    }

    /**
     * elementAt - 获取指定位置元素
     *
     * @note 从0开始计数
     */
    public void elementAt() {
        Observable
                .just(1, 2, 3, 4, 5)
                .elementAt(2)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 3
    }

    /**
     * elementAtOrDefault - 获取指定位置元素（允许默认值）
     */
    public void elementAtOrDefault() {
        Observable
                .just(1, 2, 3, 4, 5)
                .elementAtOrDefault(9, 6)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 6
    }

    /**
     * distinct - 去重
     */
    public void distinct() {
        Observable
                .just(1, 2, 3, 4, 5, 5, 5)
                .distinct()
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 1
        // call: 2
        // call: 3
        // call: 4
        // call: 5
    }

    /**
     * skip - 跳过前n项
     */
    public void skip() {
        Observable
                .just(1, 2, 3, 4, 5)
                .skip(3)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer i) {
                        Log.i(TAG, "call: " + i);
                    }
                });

        // call: 4
        // call: 5
    }

    /**
     * skipLast - 跳过后n项
     */
    public void skipLast() {
        Observable
                .just(1, 2, 3, 4, 5)
                .skipLast(3)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 1
        // call: 2
    }

    /**
     * take - 保留前n项
     */
    public void take() {
        Observable
                .just(1, 2, 3, 4, 5)
                .take(3)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 1
        // call: 2
        // call: 3
    }

    /**
     * takeLast - 保留后n项
     */
    public void takeLast() {
        Observable
                .just(1,2,3,4,5)
                .takeLast(3)
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        Log.i(TAG, "call: " + integer);
                    }
                });

        // call: 3
        // call: 4
        // call: 5
    }

    /**
     * ignoreElements - 忽略所有源Observable产生的结果
     *
     * @note 只把被观察者的onCompleted和onError事件通知给观察者
     */
    public void ignoreElements() {
        Observable
                .just(1,2,3,4,5)
                .ignoreElements()
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        Log.i(TAG, "onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG, "onError");
                    }

                    @Override
                    public void onNext(Integer integer) {
                        Log.i(TAG, "onNext: " + integer);
                    }

                    @Override
                    public void onStart() {
                        super.onStart();
                        Log.i(TAG, "onStart");
                    }
                });

        // onStart
        // onCompleted
    }
}
